/**
 * Created by hsia on 2017/3/19.
 */
var FlowprocessInfoList = function(targetObj) {
    this.targetObj = targetObj;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}
FlowprocessInfoList.prototype = {
    init : function() {
        var fieldArray = this.sysUtils.getBindFieldArray(this.targetObj.find('.search-panel'))
        var functionArray = [
            {fName : "search_btn", fs : this.findData},
            {fName : "reset_btn", fs : this.resetInfo}
        ];
        this.bindBaseModels = new bindBaseModel(null, fieldArray, functionArray, null);
        this.sysUtils.bindDoucment(this.bindBaseModels, this.targetObj, this);
        Utils.createDataDictDropDownList({
            div: this.targetObj.find('#credentialsType'),
            parentModelKey: BASIS.TRAFFIC_TYPE_CLASS.ROOT
        }, this);
        var sHeight = (this.targetObj.find('.search-panel')[0].clientHeight);
        var allHeight = (this.targetObj[0].clientHeight);
        this.targetObj.find('#flowProcess_data_list').height(allHeight-sHeight-2);
        this.initTable();
        this.initOrz();
        this.findData();
    },
    /*
     * 重置按钮
     */
    resetInfo:function(e){
        var that = $(e.target).data("that");
        that.sysUtils.setBindFieldNull(that.bindBaseModels.bindFieldArray);
    },
    initOrz:function () {
        var that = this;
        // 加载组织结构
        JsonpAjax(baseApiPath + 'recordMarket/ztreeData', '', 'json', function(res){
            var setting = {
                view: {
                    showLine: true,
                    selectedMulti: false,
                    dblClickExpand: false,
                    showIcon: false
                },
                data: {
                    key:{
                        name: "organizationName"
                    },
                    simpleData: {
                        enable:true,
                        idKey: "organizationCode",
                        pIdKey: "parentCode",
                        rootPId: ""
                    }
                }

            };
            /*var organizationTree = that.targetObj.find('#organization-tree').lonixFloat({
                source : res,
                setting : setting,
                searchField : 'organizationName',
                target : that.targetObj.find('#organization_text'),
                primary: function (nodes) {
                    that.sysUtils.mvvmModel.set('orgName',nodes[0].organizationName);
                    that.sysUtils.mvvmModel.set('orgGpc',nodes[0].organizationTypeCode);
                    that.sysUtils.mvvmModel.set('orgCode',nodes[0].organizationCode);
                    that.targetObj.find('#organization-tree').fadeUp();
                }
            });*/
        }, true);
    },
    findData : function(e) {
        var that = this;
        if (!isEmpty(e)) that = $(e.target).data("that");
        var param = that.sysUtils.getBindDocJson(that.bindBaseModels.bindFieldArray);
        //转换时间格式
        if(isNotEmpt(param["beginTime"])){
            param["beginTime"] = kendo.toString(param["beginTime"],'yyyy-MM-dd HH:mm:ss');
        }
        if(isNotEmpt(param["endTime"])){
            param["endTime"] = kendo.toString(param["endTime"],'yyyy-MM-dd HH:mm:ss');
        }
        var dataT = {jsonParam : JSON.stringify(param)};
        var dataSource = that.sysUtils.getDataSource({
            url : baseApiPath + 'flowProcess/list',
            data : dataT,
            dataType : 'odata'
        });
        var grid = that.targetObj.find('#flowProcess_data_list').data('kendoGrid');
        grid.setDataSource(dataSource);
    },
    initTable : function() {
        var that = this;
        var columns = [
            {
                width : 30,
                headerTemplate : '<input type="checkbox" gridId="flowprocesslist" onclick="gridCheckAll(this)" />',
                template : '<input type="checkbox" name="flowprocess_template_id" value="${RecordID}" />'
            },
            {
                title : '流程跟踪',
                field : 'flowName'
            },
            {
                title : '凭证编号',
                field : 'voucherNo'
            },
            {
                title : '凭证类型',
                template : '#:Utils.getModelNameByParentKey(BASIS.TRAFFIC_TYPE_CLASS.ROOT, credentialsType)#'
            },
            {
                title : '车牌号',
                field : 'vehicieNo'
            },
            {
                title : '流程开始时间',
                field : 'beginTime'
            },
            {
                title : '流程结束时间',
                field : 'endTime'
            }
        ];
        that.sysUtils.createGird(that.targetObj.find('#flowProcess_data_list'), {
            selectable : 'single',
            autoSync : true,
            columnMenu : true,
            pageable : {input : true, numeric : false},
            columns : columns,
            change : function(e) {
                var selected = $.map(this.select(), function(itm) {
                    return $(itm).find("input[type=checkbox]").val();
                });
                that.selectRowId = selected[0];
            },
            dataBound:function(){
                that.targetObj.find("#flowProcess_data_list").find("tr:gt(0)").unbind("dblclick");
                that.targetObj.find("#flowProcess_data_list").find("tr:gt(0)").dblclick( function(e) {
                    var grid = that.targetObj.find("#flowProcess_data_list").data("kendoGrid");
                    var dataItem = grid.dataItem(grid.select());
                    //打开查看窗口
                    that.openRecorderWindow(dataItem["RecordID"]);
                });
            }
        });
    },
    openRecorderWindow:function(id,isEdit){
        var that = this;
        var titles = "查看进出流程详情";
        that.editResult = null;
        that.isEdit = null;
        var openwidonw = function(){
            that.userDivId = that.sysUtils.openWindow(that.editDiv,{
                title:titles,
                height:"800px",
                width:"740px",
                resizable:false,
                content:"views/parkLotCenter/recordMarket/flowProcessEdit.html"
            }, true);
        };
        var ajaxPost = function(){
            JsonpAjax(baseApiPath+"flowProcess/history?recordId=" + id,null,"json",function(result){
                that.editResult = result;
                openwidonw();
            });
        };
        that.isEdit = false;
        ajaxPost();
    },
}

var FlowprocessInfoEdit = function(targetObj) {
    this.targetObj = targetObj;
    this.sysUtils = new kendoUtils();

    this.init();
    return this;
}
FlowprocessInfoEdit.prototype = {
    init : function() {
        var that = this;
        var fieldArray = this.sysUtils.getBindFieldArray(this.targetObj);
        var functionArray = [
        ];
        this.bindBaseModels = new bindBaseModel(null, fieldArray, functionArray, null);
        this.sysUtils.bindDoucment(this.bindBaseModels, this.targetObj, this);
        this.loadInfo();
    },
    loadInfo:function () {
        var that = this;
        var entitys=flowprocessManager.editResult;
        if(!isEmpty(entitys)){
            var idArray=new Array();
            for(var i= 0,l=entitys.length;i<l;i++){
                idArray.push(entitys[i].parentId)
            }
            Array.prototype.unique = function(){
                var res = [];
                var json = {};
                for(var i = 0; i < this.length; i++){
                    if(!json[this[i]]){
                        res.push(this[i]);
                        json[this[i]] = 1;
                    }
                }
                return res;
            }
            idArray=idArray.unique();
            var html='';
            for(var i=0,l=idArray.length;i<l;i++){
                var firstHtml='';
                for(var t= 0,s=entitys.length;t<s;t++){
                    if(idArray[i]==entitys[t].parentId){
                        if(firstHtml==''){
                            firstHtml+='<div class="year">'
                                +'    <h2><a href="#">'+entitys[t].parentName+'<i></i></a></h2>'
                                +'    <div class="list">'
                                +'        <ul >'
                                +'            <li class="cls">'
                                +'                <p class="date">'+entitys[t].showTime+'</p>'
                                +'                <p class="intro">'+entitys[t].nodeName+'</p>'
                                +'                <p class="version">&nbsp;</p>'
                                +'                <div class="more">'
                                +'                    <p>'+entitys[t].Remark+'</p>'
                                +'                </div>'
                                +'            </li>';
                        }else{
                            firstHtml+='      <li class="cls">'
                                +'                <p class="date">'+entitys[t].showTime+'</p>'
                                +'                <p class="intro">'+entitys[t].nodeName+'</p>'
                                +'                <p class="version">&nbsp;</p>'
                                +'                <div class="more">'
                                +'                    <p>'+entitys[t].Remark+'</p>'
                                +'                </div>'
                                +'            </li>';
                        }
                    }
                }
                firstHtml+='        </ul>'
                    +'    </div>'
                    +'</div>';
                html+=firstHtml;
            }
            that.targetObj.find('#mainboard').append(html);
            that.targetObj.find(".main .year .list").each(function (e, target) {
                var $target=  $(target),
                    $ul = $target.find("ul");
                $target.height($ul.outerHeight()), $ul.css("position", "absolute");
            });
            that.targetObj.find(".main .year>h2>a").click(function (e) {
                e.preventDefault();
                $(this).parents(".year").toggleClass("close");
            });
        }
    }
}